#include<iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>

// 使用命名空间别名，避免污染全局命名空间
namespace ba = boost::algorithm;
void demonstrate_string_algorithms() {
    std::cout << "=== Boost字符串算法示例 ===\n" << std::endl;
    
    // 1. 大小写转换
    std::string str1 = "Hello Boost Library";
    std::cout << "1. 大小写转换:" << std::endl;
    std::cout << "原始字符串: " << str1 << std::endl;
    std::cout << "转大写: " << ba::to_upper_copy(str1) << std::endl;
    std::cout << "转小写: " << ba::to_lower_copy(str1) << std::endl;
    std::cout << std::endl;
    
    // 2. 字符串修剪
    std::string str2 = "   Hello World!   ";
    std::string str3 = "***Hello Boost***";
    std::cout << "2. 字符串修剪:" << std::endl;
    std::cout << "原始字符串: '" << str2 << "'" << std::endl;
    std::cout << "去除两端空格: '" << ba::trim_copy(str2) << "'" << std::endl;
    std::cout << "去除左端空格: '" << ba::trim_left_copy(str2) << "'" << std::endl;
    std::cout << "去除右端空格: '" << ba::trim_right_copy(str2) << "'" << std::endl;
    
    // 使用谓词修剪特定字符
    std::cout << "去除特定字符: '" << ba::trim_copy_if(str3, ba::is_any_of("*")) << "'" << std::endl;
    std::cout << std::endl;
    
    // 3. 字符串查找和判断
    std::string str4 = "Hello Boost C++ Library";
    std::cout << "3. 字符串查找和判断:" << std::endl;
    std::cout << "字符串: " << str4 << std::endl;
    std::cout << "是否以'Hello'开头: " << std::boolalpha << ba::starts_with(str4, "Hello") << std::endl;
    std::cout << "是否以'Library'结尾: " << ba::ends_with(str4, "Library") << std::endl;
    std::cout << "是否包含'Boost': " << ba::contains(str4, "Boost") << std::endl;
    std::cout << std::endl;
    
    // 4. 字符串替换
    std::string str5 = "I like apples and apples are good";
    std::cout << "4. 字符串替换:" << std::endl;
    std::cout << "原始字符串: " << str5 << std::endl;
    std::cout << "替换所有'apples': " << ba::replace_all_copy(str5, "apples", "oranges") << std::endl;
    std::cout << "替换第一个'apples': " << ba::replace_first_copy(str5, "apples", "oranges") << std::endl;
    std::cout << "替换最后一个'apples': " << ba::replace_last_copy(str5, "apples", "oranges") << std::endl;
    std::cout << std::endl;
    
    // 5. 字符串分割
    std::string str6 = "apple,banana,orange,grape";
    std::string str7 = "one|two|three";
    std::cout << "5. 字符串分割:" << std::endl;
    std::cout << "原始字符串: " << str6 << std::endl;
    
    std::vector<std::string> tokens;
    ba::split(tokens, str6, ba::is_any_of(","));
    
    std::cout << "分割结果: ";
    for (size_t i = 0; i < tokens.size(); ++i) {
        std::cout << "'" << tokens[i] << "'";
        if (i < tokens.size() - 1) std::cout << ", ";
    }
    std::cout << std::endl;
    
    // 使用tokenizer分割
    tokens.clear();
    ba::split(tokens, str7, ba::is_any_of("|"));
    std::cout << "另一种分割: ";
    for (const auto& token : tokens) {
        std::cout << token << " ";
    }
    std::cout << std::endl << std::endl;
    
    // 6. 字符串连接
    std::vector<std::string> fruits = {"apple", "banana", "orange"};
    std::cout << "6. 字符串连接:" << std::endl;
    std::cout << "连接结果: " << ba::join(fruits, ", ") << std::endl;
    std::cout << "带前缀连接: " << ba::join(fruits, " and ") << std::endl;
    std::cout << std::endl;
    
    // 7. 字符串比较（不区分大小写）
    std::string str8 = "HELLO";
    std::string str9 = "hello";
    std::cout << "7. 字符串比较:" << std::endl;
    std::cout << "比较 '" << str8 << "' 和 '" << str9 << "':" << std::endl;
    std::cout << "区分大小写比较: " << std::boolalpha << (str8 == str9) << std::endl;
    std::cout << "不区分大小写比较: " << ba::iequals(str8, str9) << std::endl;
    std::cout << std::endl;
    
    // 8. 字符串查找和提取
    std::string str10 = "The quick brown fox jumps over the lazy dog";
    std::cout << "8. 字符串查找和提取:" << std::endl;
    std::cout << "字符串: " << str10 << std::endl;
    
    if (ba::find_first(str10, "fox")) {
        auto result = ba::find_first(str10, "fox");
        std::cout << "找到'fox'在位置: " << (result.begin() - str10.begin()) << std::endl;
        std::cout << "提取匹配部分: " << result << std::endl;
    }
    
    // 9. 谓词函数的使用
    std::string str11 = "Hello123";
    std::cout << "9. 字符串谓词检查:" << std::endl;
    std::cout << "字符串 '" << str11 << "':" << std::endl;
    std::cout << "是否全为字母: " << ba::all(str11, ba::is_alpha()) << std::endl;
    std::cout << "是否全为数字: " << ba::all(str11, ba::is_digit()) << std::endl;
    std::cout << "是否以字母开头: " << ba::all(str11.substr(0, 1), ba::is_alpha()) << std::endl;
}

int main() {
    demonstrate_string_algorithms();
    return 0;
}
